
#include <iostream>
#include <vector>
#include <algorithm>

class Solution
{
public:
    using PII = pair<int, int>;
    int knapsack(int V, int n, vector<vector<int>>& vw)
    {
        int res = 0;
        int curv = 0, curw = 0;
        vector<PII> vp;

        for (const auto& e : vw)
        {
            vp.push_back({ e[0], e[1] });
        }

        auto curvp = vp;

        for (const auto& e : vp)
        {
            if (curv + e.first <= V)
            {
                curv += e.first;
                curw += e.second;
                curvp.push_back(e);
            }
            else
            {
                PII tmp{ INT_MAX, 0 };
                int idx = -1;
                for (int i = 0, n = curvp.size(); i < n; ++i)
                {
                    if (e.first <= curvp[i].first && e.second > curvp[i].second)
                    {
                        if ((e.first <= tmp.first && e.second > tmp.second) || (e.first > tmp.first && e.second > tmp.second))
                        {
                            tmp = curvp[i];
                            idx = i;
                        }
                    }
                }

                if (-1 != idx)
                {
                    curv -= curvp[idx].first - e.first;
                    curw += e.second - curvp[idx].second;
                    curvp[idx] = e;
                }
            }
            res = max(res, curw);
        }

        return res;
    }
};

void Test1()
{
    vector<vector<int>> v;
}

int main()
{
    return 0;
}